package com.spzx.test1.Day3Test;
/*
    并非全力
*/

import com.spzx.common.core.domain.R;

import java.sql.SQLOutput;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Supplier;

//public class t5 {
//    public static void main(String[] args) throws InterruptedException {
//
//        ThreadPoolExecutor pool = new ThreadPoolExecutor(10,
//                10,
//                0L,
//                TimeUnit.MILLISECONDS,
//                new LinkedBlockingDeque<>(1024));
//
//        System.out.println(Thread.currentThread().getName() + "主线程开始执行");
//
//        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(new Runnable() {
//            @Override
//            public void run() {
//                System.out.println("执行无返回值任务");
//            }
//        },pool);
//        CompletableFuture<Integer> returnCompletableFuture = CompletableFuture.supplyAsync(new Supplier<Integer>() {
//            @Override
//            public Integer get() {
//                System.out.println(Thread.currentThread().getName() + "执行有返回值任务");
//                return 100;
//            }
//        },pool);
//        CompletableFuture<Integer> thenCompletableFuture1  = returnCompletableFuture.thenApplyAsync(new Function<Integer, Integer>() {
//            @Override
//            public Integer apply(Integer integer) {
//                System.out.println(Thread.currentThread().
//                        getName() + "我是completableFuture的接力线程，我得到一个值：" + integer);
//                Integer a = integer * 2;
//                return a;
//            }
//        },pool);
//
//
//        Thread.sleep(2000);
//        System.out.println(Thread.currentThread().getName() + "主线程执行结束");
//
//
//    }
//}
//
//
//
//
//
public class t5 {
    public static void main(String[] args) throws InterruptedException {
        // 创建一个线程池
        ThreadPoolExecutor pool = new ThreadPoolExecutor(10,
                10,
                0L,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingDeque<>(1024));

        // 主线程开始执行
        System.out.println(Thread.currentThread().getName() + "主线程开始执行");

        // 异步执行一个无返回值的任务
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                System.out.println("执行无返回值任务");
            }
        }, pool);

        // 异步执行一个有返回值的任务
        CompletableFuture<Integer> returnCompletableFuture = CompletableFuture.supplyAsync(new Supplier<Integer>() {
            @Override
            public Integer get() {
                System.out.println(Thread.currentThread().getName() + "执行有返回值任务");
                return 100;
            }
        }, pool);

        // 在有返回值的任务完成后，异步应用一个函数
        CompletableFuture<Integer> thenCompletableFuture1  = returnCompletableFuture.thenApplyAsync(new Function<Integer, Integer>() {
            @Override
            public Integer apply(Integer integer) {
                System.out.println(Thread.currentThread().
                        getName() + "我是completableFuture的接力线程，我得到一个值：" + integer);
                Integer a = integer * 2;
                return a;
            }
        }, pool);

        // 主线程等待一段时间
        Thread.sleep(2000);

        // 主线程执行结束
        System.out.println(Thread.currentThread().getName() + "主线程执行结束");
    }
}